Ext.define('PHT.CheckDpropAvailableGrid', {
    extend: 'Ext.grid.Panel',
    alias: ['widget.chkdpropserveravailgrid'], 
    
    id: 'dproper-available-grid',
    hideHeaders: true,
    checkDpropcfgwin:'undefined',
    
    onAddClick: function(){
    	if(this.checkDpropcfgwin == 'undefined'){
    		this.checkDpropcfgwin = new PHT.CheckDpropServerCfgAddWindow();
    	}
    	var add = this.down('#adddprop');
    	add.setDisabled(true);
    	if(this.checkDpropcfgwin.isVisible()){
    		this.checkDpropcfgwin.hide(this,function(){
    			add.setDisabled(false);
    		});
    	}else{
    		this.checkDpropcfgwin.show(this,function(){
    			add.setDisabled(false);
    		});
    	}  
    	
    	var form = this.checkDpropcfgwin.down('#add-dprop-server-form');
		form.ismodified = false;
    },
    
    onSelectChange: function(selModel, selections){
    	this.down('#modify').setDisabled(selections.length === 0);
        this.down('#delete').setDisabled(selections.length === 0);
        //this.down('#apply').setDisabled(selections.length === 0);
        if(this.checkDpropcfgwin == 'undefined'){
    		this.checkDpropcfgwin = new PHT.CheckDpropServerCfgAddWindow();
    	}
        var form = this.checkDpropcfgwin.down('#add-dprop-server-form');
        form.setActiveRecord(selections[0]||null);
    },
    
    onDeleteClick: function(){
        var selection = this.getView().getSelectionModel().getSelection()[0];
        if (selection) {
            this.store.remove(selection);
        }
    },
    
    onModifyClick:function(){
    	if(this.checkDpropcfgwin == 'undefined'){
    		this.checkDpropcfgwin = new PHT.CheckDpropServerCfgAddWindow();
    	}
    	var selection = this.getView().getSelectionModel().getSelection()[0];
    	if(selection){
    		var form = this.checkDpropcfgwin.down('#add-dprop-server-form');
    		form.ismodified = true;
    		form.getForm().loadRecord(selection);
    		form.setActiveRecord(selection||null);
    		this.checkDpropcfgwin.show();
    	}   	
    },
    
    onApplyClick: function(){
    	var store = Ext.data.StoreManager.lookup('available-dprop-server-store');
    	store.sync();
    },

    initComponent: function() {
        var me = this;
        
        Ext.applyIf(me, {
        	dockedItems: [{
                xtype: 'toolbar',
                dock: 'bottom',
                items: [{
                    //iconCls: 'icon-add',
                    text: 'Add',
                    itemId: 'adddprop',
                    id: 'add-dprop-sverer-id',
                    scope: this,
                    handler: this.onAddClick
                },{
                    //iconCls: 'icon-delete',
                    text: 'Modify',
                    disabled: true,
                    itemId: 'modify',
                    scope: this,
                    handler: this.onModifyClick
                },{
                    //iconCls: 'icon-delete',
                    text: 'Delete',
                    disabled: true,
                    itemId: 'delete',
                    scope: this,
                    handler: this.onDeleteClick
                },{
                    //iconCls: 'icon-delete',
                    text: 'Apply',
                    //disabled: true,
                    itemId: 'apply',
                    scope: this,
                    handler: this.onApplyClick
                }]
            }],
            columns: [
                {
                    xtype: 'gridcolumn',
                    dataIndex: 'alias',
                    flex: 1
                },{
                	xtype: 'gridcolumn',
                    dataIndex: 'port',
                    hidden: true
                },{
                	xtype: 'gridcolumn',
                    dataIndex: 'hostname',
                    hidden: true
                },{
                	xtype: 'gridcolumn',
                    dataIndex: 'username',
                    hidden: true
                },{
                	xtype: 'gridcolumn',
                    dataIndex: 'password',
                    hidden: true
                },{
                	xtype: 'gridcolumn',
                    dataIndex: 'id',
                    hidden: true
                }            
            ],
            store:Ext.create('Ext.data.Store', {
            	model: 'PHT.MODEL.DpropServerItem',
                storeId:'available-dprop-server-store',
                proxy: {
                    type: 'ajax',
                    reader: {
                        type: 'json',
                        root: 'servers',
                        successProperty: 'success'
                    },
                    api: {
                        read: 'dpropserverread.do?n='+ new Date(),
                        create: 'dpropservercreate.do?n='+ new Date(),
                        update: 'dpropserverupdate.do?n=' + new Date(),
                        destroy: 'dpropserverdestroy.do?n=' + new Date()
                    },
                    writer: {
                    	type: 'json',
                        writeAllFields: true,
                        root: 'servers'
                    }
                },
                autoLoad: true
            }),
        	frame: true,
        });        
        me.callParent(arguments);
        this.getSelectionModel().on('selectionchange', this.onSelectChange, this);
    }
});